package it.unibo.iss.domotica;

import android.content.Context;
import android.content.Intent;
import android.app.Activity;
import android.os.IInterface;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;

/**
 * @author AN DEIS Unibo
 */
public abstract class UsrCmdActivitySupport extends BaseActivity {

	protected String inputValue = "TODO"; //Last value inserted by the user
	protected String messageInput = "TODO"; //Last value received via an intent
	protected String content = "TODO"; //Last value of some application content

	//ExecutorVar [null]

	// Connectors to services used by the action

	//Reference to System Services 

	/* ----------------------------------
	 * System level methods
	 *  ----------------------------------
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usrcmdactivitylayout);
		traceMsg("onCreate ... " + this.getLocalClassName());
		/* ----------------------------------
		 * Create connection support objects
		 *  ----------------------------------
		 */
		/* ----------------------------------
		 * Configure system services
		 *  ----------------------------------
		 */

		/* ----------------------------------
		 * Create user cmd buttons
		 *  ----------------------------------
		 */

		Button updateButton = (Button) findViewById(R.id.update);

		updateButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					updateListEd();
				} catch (Exception e) {
				}
			}
		});

		/* ----------------------------------
		 * CStart all the executors subject working for this activity
		 *  ----------------------------------
		 */

	}//onCreate  	

	protected void onResume() {
		super.onResume();
		/* ----------------------------------
		 * Connect to services
		 *  ----------------------------------
		 */

		/* ----------------------------------
		 * Look at input and work
		 *  ----------------------------------
		 */
		lookAtInput();
	}

	protected void onPause() {
		super.onPause();
		/* ----------------------------------
		 * UnSet connection support objects
		 *  ----------------------------------
		 */

	}

	/* ----------------------------------
	 * Actions  
	 *  ----------------------------------
	 */

	//action WITHOUT answer  
	protected abstract void updateListEd(

	) throws Exception;

	/* ----------------------------------
	 * Explicit interactions
	 *  ----------------------------------
	 */

	/* ----------------------------------
	 * Contact interactions
	 *  ----------------------------------
	 */

	/* -------------------------------------
	 *	INPUT
	 * -------------------------------------
	 */
	//Find the activity input in order to decide what to do
	protected void lookAtInput() {
		Intent sourceIntent = getIntent();
		//		traceMsg("UsrCmdActivity received \n" + intentToString(sourceIntent) );
		if (sourceIntent != null && sourceIntent.getAction() != null)
			analyseInput(sourceIntent);
	}

	//Check the nature of the activity input
	protected void analyseInput(Intent i) {
		if (i.getAction().contains("action.MAIN"))
			doJob();
		else
			execAction(i);
	}

	//Perform some local job
	protected void doJob() {
		traceMsg("UsrCmdActivity local job (TODO)");
	}

	//Execute some action
	protected void execAction(Intent input) {
		try {
			//println("UsrCmdActivity execAction "  + input.getAction() );

			if (input.getAction().equals("update")) {

				updateListEd();
			}

		} catch (Exception e) {
		}
	}

}
